const Router =require('koa-router') 
const requireDirectory =require('require-directory')
class InitManager{
    static initCore(app){
        //入口方法-调用
        InitManager.app=app
        InitManager.initLoadRouters() 
        InitManager.loadHTTPException() //开始就运行挂载
        InitManager.loadConfig() //开始就运行挂载
    }

    static loadConfig(path=''){
        const configPath = path||process.cwd() + '/config/config.js'
        const config= require(configPath)
        global.config=config  //放进全局变量方便调用
    }

    static initLoadRouters(){
        const apiDirectory = `${process.cwd()}/app/api`
        requireDirectory(module,apiDirectory,{visit:whenLoadModule})

        function whenLoadModule(obj) {
            if (obj instanceof Router) {  
                InitManager.app.use(obj.routes()) 
            }   
        }
    }

    static loadHTTPException(){
        const errors =require('./http-exception')
        global.errs=errors  //挂载该文件所有内容在全局global对象变量上
    }
}

module.exports = InitManager